import java.util.concurrent.TimeUnit;

/**
 * Create by chenhd on 2021/8/28.
 * 方法一：使用一个变量标识是否子线程执行完成，类似自旋锁循环判断
 */
public class ConcurrentTask1 {
    public boolean isFinish = false;
    public String str;

    public static void main(String[] args) throws InterruptedException {
        ConcurrentTask1 concurrentTask = new ConcurrentTask1();

        new Thread(concurrentTask::putStr).start();

        System.out.println(concurrentTask.getStr());
    }

    public void putStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.isFinish = true;
            this.str = "hello concurrent.";
        } catch (InterruptedException e) {
            //log error
            e.printStackTrace();
        }
    }

    public String getStr() throws InterruptedException {
        while (!this.isFinish) {
            TimeUnit.MILLISECONDS.sleep(10);
        }
        return str;
    }
}
